<?php

declare (strict_types = 1);

namespace app\model\BaseModel;

use think\Model;
use think\facade\Db;
class BaseModel extends Model
{
    /**
     * instance
     * @var    array
     */
    private static $instance;
    
    /**
     * @get instance
     * @return   static
     */
    public static function instance()
    {
        $class_name = get_called_class();
        if (empty(self::$instance[$class_name])) {
            self::$instance[$class_name] = new static();
        }
        return self::$instance[$class_name];
    }
    /**
     * @按条件获取一条数据
     * @param    array  $where
     * @access   public
     * @return   array
     */
    public function info($where = []):array
    {
        $info = $this->_dealWhere($where)->find();
        if (empty($info)) {
            return [];
        }

        return $info->toArray();
    }

    /**
     * @获取多条数据
     * @param    array  $where
     * @access   public
     * @return   array
     */
    public function lists($where = []):array
    {
        $lists = $this->_dealWhere($where)->select();

        if (empty($lists)) {
            return [];
        }

        return $lists->toArray();
    }

    /**
     * @按页获取数据
     * @param    array  $where
     * @param    int    $limit
     * @param    int    $page
     * @access   public
     * @return   array
     */
    public function listPages($where = [],  int $limit = 20,  int $page = 1):array
    {
        $db = $this->_dealWhere($where);

        $offset = $limit * ($page - 1);
        $offset < 0 && $offset = 0;
        $limit <= 0 && $limit = 20;

        $db->limit($offset, $limit);

        $lists  =  $db->select();

        if (empty($lists)) {
            return [];
        }

        return $lists->toArray();
    }
    /**
     * @计算数据行数
     * @param    array  $where
     * @access   public
     * @return   int
     */
    public function countNum($where = null)
    {
        return $this->_dealWhere($where)->count();
    }

    /**
     * @添加
     * @param    array $data
     * @access   public
     * @return   int
     */
    public function add($data)
    {
        return $this->insertGetId($data);
    }

    /**
     * @删除
     * @param    array $where
     * @access   public
     * @return   int
     */
    public function del($where = [])
    {
        return $this->_dealWhere($where)->delete();
    }

    /**
     *@des      修改数据
     *@param    array  $where  条件
     *@param    array  $data   数据
     *@access   public
     *@return   mixed
     */
    public function edit($where, $data)
    {
        return $this->where($where)->update($data);
    }

    /**
     *@des      条件生成
     *@param    array  $params //查询条件
     *@return   Query
     */
    private function _dealWhere($params = null){
        $db = $this->db();

        if(isset($params['_fields_'])){
            $db->field($params['_fields_']);
            unset($params['_fields_']);
        }

        if(isset($params['_limit_'])){
            if (is_array($params['_limit_']) && isset($params['_limit_'][0]) && $params['_limit_'][1]) {
                $db->limit($params['_limit_'][0], $params['_limit_'][1]);
            } else {
                $db->limit($params['_limit_']);
            }
            unset($params['_limit_']);
        }

        if (isset($params['_order_'])) {
            if (is_array($params['_order_'])) {
                $order  =  array();
                foreach ($params['_order_'] as $key=>$value) {
                    $order[]  =  $key . ' ' . $value;
                }
                $db->order(implode($order, ','));
            } else {
                $db->order($params['_order_']);
            }
            unset($params['_order_']);
        }

        if (isset($params['_orderRaw_'])) {
            if (is_array($params['_orderRaw_'])) {

                $order  =  array();
                foreach ($params['_orderRaw_'] as $key=>$value) {
                    $order[]  =  $key . ' ' . $value;
                }
                $db->orderRaw(implode($order, ','));
            } else {
                $db->orderRaw($params['_orderRaw_']);
            }
            unset($params['_orderRaw_']);
        }

        if(isset($params['_groupBy_'])){
            if (is_array($params['_groupBy_'])) {

                $groupBy  =  array();
                foreach ($params['_groupBy_'] as $key=>$value) {
                    $groupBy[]  =  $key . ' ' . $value;
                }
                $db->group(implode($groupBy, ','));
            } else {
                $db->group($params['_groupBy_']);
            }
            unset($params['_groupBy_']);
        }

        if(!empty($params)){
            $db->where($params);
        }
        return $db;
    }
    
    /**
     * 开启事务
     */
    public function startTrans() 
    {
        Db::startTrans();
    }
    
    /**
     * 事务回滚
     */
    public function rollback()
    {
        Db::rollback();
    }
    
    /**
     * 事务提交
     */
    public function commit()
    {
        Db::commit();
    }
}